#coding:utf-8
import scrapy
import json
import csvout

class QuotesSpider(scrapy.Spider):
    name = "bing_academic"

    def start_requests(self):
        url = 'http://cn.bing.com/academic'
        tag = getattr(self, 'tag', None)

        if tag is not None:
            url = url + '/search?q=' + tag 
            yield scrapy.Request(url, callback=self.parse)

    def parse(self, response):
        #从chrome里拷出来的xpath
        res = response.xpath('//*[@id="b_results"]/li[12]/span/text()').extract_first()
        #过滤出其中的数字
        num = filter(lambda x: x.isdigit(), res)
        num = int(num)
        outdic = {
            'bing_academic_num': num
        }
        outli = ['bing_academic_num', num, response.url]
        csvout.list2csv(outli)
        yield outdic
